package com.cheng.operation.impl;

import com.cheng.common.exception.CommonException;
import com.cheng.common.utils.CodeUtil;
import com.cheng.common.utils.DateUtil;
import com.cheng.common.utils.StringUtil;
import com.cheng.data.entity.AcceptInfoEntity;
import com.cheng.data.entity.PermInfoEntity;
import com.cheng.data.vo.*;
import com.cheng.operation.PermInfoOperation;
import com.cheng.service.PermInfoService;
import com.cheng.service.RoleInfoService;
import com.cheng.service.UserInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author: ChengJW
 * @date: 2021/11/13 17:51
 * @version: 1.0
 * @description: 权限信息表业务逻辑处理类
 */
@Service
public class PermInfoOperationImpl implements PermInfoOperation {

    private final static Logger log = LoggerFactory.getLogger(PermInfoOperationImpl.class);

    @Autowired
    private PermInfoService permInfoService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private RoleInfoService roleInfoService;

    /**
    * @author: ChengJW
    * @params: []
    * @return: com.cheng.data.vo.RespPageInfoVO
    * @date: 2021/11/13
    * @description: 查询所有的权限信息，并分页返回
    */
    @Override
    public RespPageInfoVO queryPermInfoList(ReqPageInfoVO pageVO) throws CommonException {

        //对查询结果进行分页并初始化分页信息
        PermInfoVO permInfoVO = new PermInfoVO();
        //第一个查询条件参数传过来的值是一级菜单代码
        permInfoVO.setPrUpsLevelNm(String.valueOf(pageVO.getQryReqFir()));
        //第二个查询条件参数传过来的值是二级菜单代码
        permInfoVO.setPrLevelNm(String.valueOf(pageVO.getQryReqSeco()));
        //第三个查询条件参数传过来的值是菜单名称(中文描述)
        permInfoVO.setPrDesc(String.valueOf(pageVO.getQryReqThird()));
        PageInfo<PermInfoEntity> pageInfo = PageHelper.startPage(pageVO.getPageNum(),pageVO.getPageSize()).doSelectPageInfo(() -> permInfoService.queryPermInfoList(permInfoVO));
        List<PermInfoEntity> list = pageInfo.getList();
        List<PermInfoVO> permInfoVOList = new ArrayList<>();
        if (null != list && list.size() > 0){
            for (PermInfoEntity entity : list){
                PermInfoVO vo = new PermInfoVO();
                BeanUtils.copyProperties(entity,vo);
                //entity跟vo的stDt的类型不一样,所以需要单独设值
                vo.setStDt(DateUtil.dateToStr(entity.getStDt(), CodeUtil.DATE_STRING));
                permInfoVOList.add(vo);
            }
        }
        RespPageInfoVO respPageInfoVO = new RespPageInfoVO();
        respPageInfoVO.setList(permInfoVOList);
        respPageInfoVO.setPageNum(pageInfo.getPageNum());
        respPageInfoVO.setPageSize(pageInfo.getPageSize());
        respPageInfoVO.setPageCount(pageInfo.getTotal());
        return respPageInfoVO;
    }

    /**
    * @author: ChengJW
    * @params: [name 查询条件, pdTpCd 查询类型：01 用户权限查询；02 角色权限查询]
    * @return: java.util.List<com.cheng.data.vo.TreeVO>
    * @date: 2021/11/16
    * @description: 查询所有的权限信息及用户、角色的权限信息，并按照 layui tree 要求的数据源格式返回
    */
    @Override
    public List<TreeVO> queryAllPermInfoList(String name,String pdTpCd) throws CommonException {

        //先查出所有的一级菜单权限信息
        List<PermInfoEntity> lvl1PermList = permInfoService.queryPermListForTree(null,"01");
        List<String> permIdList = new ArrayList<>();
        if ("01".equals(pdTpCd) && StringUtil.isNotNull(name)){
            //根据用户名称，查询用户的所有菜单权限ID
            UserInfoVO userInfoVO = userInfoService.queryUserBaseInfoByNm(name);
            if (null != userInfoVO){
                List<PermInfoVO> list = userInfoVO.getPermInfoVOList();
                if (null != list && list.size() > 0){
                    for (PermInfoVO vo : list){
                        String permId = vo.getPrId();
                        permIdList.add(permId);
                    }
                }
            }
        }else if ("02".equals(pdTpCd) && StringUtil.isNotNull(name)){
            //根据角色ID，查询用户的所有菜单权限ID
            RoleInfoVO vo = new RoleInfoVO();
            vo.setRoId(name);
            List<PermInfoVO> list = roleInfoService.queryAllPermInfoByRoleInfo(vo);
            if (null != list && list.size() > 0){
                for (PermInfoVO permInfoVO : list){
                    permIdList.add(permInfoVO.getPrId());
                }
            }
        }
        List<TreeVO> lvl1TreeVOList = new ArrayList<>();
        if (null != lvl1PermList && lvl1PermList.size() > 0){
            //通过一级菜单权限，查询各自的二级菜单权限信息
            for (PermInfoEntity vo1 : lvl1PermList) {
                String prUpsLvlNm = vo1.getPrUpsLevelNm();
                //二级菜单权限信息(包含对应的一级菜单)
                List<PermInfoEntity> lvl2PermList = permInfoService.queryPermListForTree(prUpsLvlNm, "02");
                List<TreeVO> lvl2Children = new ArrayList<>();
                Iterator<PermInfoEntity> iterable = lvl2PermList.iterator();
                while (iterable.hasNext()) {
                    PermInfoEntity vo2 = iterable.next();
                    //遍历二级菜单权限信息，并添加到集合中
                    if (StringUtil.isNull(vo2.getPrLevelNm())) {
                        //将二级菜单集合中的一级菜单去除
                        iterable.remove();
                        continue;
                    }
                    TreeVO lvl2TreeVO = new TreeVO();
                    lvl2TreeVO.setId(vo2.getPrId());
                    lvl2TreeVO.setTitle(vo2.getPrDesc());
                    lvl2TreeVO.setHref(vo2.getPrUrl());
                    //二级菜单没有下属菜单
                    lvl2TreeVO.setChildren(null);
                    //设置用户二级菜单选中状态
                    if (permIdList.contains(vo1.getPrId())){
                        lvl2TreeVO.setChecked(true);
                    }
                    lvl2Children.add(lvl2TreeVO);
                }
                TreeVO lvl1TreeVO = new TreeVO();
                lvl1TreeVO.setId(vo1.getPrId());
                lvl1TreeVO.setTitle(vo1.getPrDesc());
                lvl1TreeVO.setHref(vo1.getPrUrl());
                lvl1TreeVO.setChildren(lvl2Children);
                //设置用户一级级菜单选中状态
                if (permIdList.contains(vo1.getPrId())){
                    lvl1TreeVO.setChecked(true);
                }
                //将一级菜单权限存入集合中
                lvl1TreeVOList.add(lvl1TreeVO);
            }
        }
        return lvl1TreeVOList;
    }
}
